package com.laozhao.wintersystem;

import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.laozhao.component.DefaultMenuListener;
import com.laozhao.component.DefaultTaskListener;

public class WListenersModel {

	public static final String DEFAULT_TASK_LISTENER_ID = "Default_Task_Listener_Id";
	public static final String DEFAULT_MENU_LISTENER_ID = "Default_Menu_Listener_Id";

	private Map<String, WListener> map;

	private final String ATTLIST_LISTENER_ID = "id";
	private final String ATTLIST_LISTENER_CLASS = "class";
	private final String ATTLIST_LISTENER_UNIQUE = "unique";

	public WListenersModel(Node node) {
		load(node);
	}

	public ActionListener getListener(String id) {
		if (map == null) {
			return map.get(id).getListener();
		} else {
			return null;
		}
	}

	private void load(Node node) {
		map = new HashMap<String, WListener>();
		map.put(DEFAULT_MENU_LISTENER_ID,  new WListener(DefaultMenuListener.class, false));
		map.put(DEFAULT_TASK_LISTENER_ID,  new WListener(DefaultTaskListener.class, false));
		if (node != null) {
			// <!ELEMENT listeners(listener+)>
			NodeList listenersNodes = node.getChildNodes();
			for (int k = 0; k < listenersNodes.getLength(); k++) {
				Node listener = listenersNodes.item(k);
				String tmpId = WSTools.getStringAttribute(listener,
						ATTLIST_LISTENER_ID);
				String tmpClass = WSTools.getStringAttribute(listener,
						ATTLIST_LISTENER_CLASS);
				boolean unique = WSTools.getBooleanAttribute(listener,
						ATTLIST_LISTENER_UNIQUE);
				
				map.put(tmpId, new WListener(tmpClass, unique));
			}
		}
//		try {
			// if (system != null) {
			// // <!ELEMENT system (splash?,frame+,tray)>
			// NodeList systemNodes = system.getChildNodes();
			// for (int i = 0; i < systemNodes.getLength(); i++) {
			// Node frame = systemNodes.item(i);
			// if (frame.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE
			// && frame.getNodeName().equalsIgnoreCase(
			// ELEMENT_SYSTEM_FRAME)) {
			// // <!ELEMENT frame
			// // (listeners?,menubar?,tasks?,pages,statusbar?)>
			// NodeList frameNodes = frame.getChildNodes();
			// for (int j = 0; j < frameNodes.getLength(); j++) {
			// Node listeners = frameNodes.item(j);
			// if (listeners.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE
			// && listeners.getNodeName()
			// .equalsIgnoreCase(
			// ELEMENT_FRAME_LISTENERS)) {
			// // <!ELEMENT listeners(listener+)>
			// NodeList listenersNodes = listeners
			// .getChildNodes();
			// for (int k = 0; k < listenersNodes.getLength(); k++) {
			// Node listener = listenersNodes.item(j);
			// String tmpId = WSTools.getStringAttribute(
			// listener, ATTLIST_LISTENER_ID);
			// String tmpClass = WSTools
			// .getStringAttribute(listener,
			// ATTLIST_LISTENER_CLASS);
			// boolean unique = WSTools
			// .getBooleanAttribute(listener,
			// ATTLIST_LISTENER_UNIQUE);
			// Class<? extends ActionListener> tmpClazz = (Class<? extends
			// ActionListener>) Class
			// .forName(tmpClass);
			// map.put(tmpId, new WListenersModel(tmpId,
			// tmpClazz, unique));
			// }
			// }
			// }
			// }
			//
			// }
			// }
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
	}

	private class WListener {
		private Class<? extends ActionListener> clazz;
		private boolean unique = false;
		ActionListener tmpListener;

		@SuppressWarnings("unchecked")
		private WListener(String clazz, Boolean unique) {
			try {
				this.clazz = (Class<? extends ActionListener>) Class
				.forName(clazz);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			this.unique = unique;
			if (unique) {
				tmpListener = toListener();
			}
		}
		
		private WListener(Class<? extends ActionListener> clazz, Boolean unique) {
			this.clazz = clazz;
			this.unique = unique;
			if (unique) {
				tmpListener = toListener();
			}
		}

		private ActionListener getListener() {
			if (unique) {
				return tmpListener;
			} else {
				return toListener();
			}
		}

		private ActionListener toListener() {
			try {
				ActionListener listener = clazz.newInstance();
				return listener;
			} catch (InstantiationException e) {
				e.printStackTrace();
				throw new RuntimeException(
						"actionlistener class must extends ActionListener");
			} catch (IllegalAccessException e) {
				e.printStackTrace();
				throw new RuntimeException(
						"actionlistener class must extends ActionListener");
			}
		}
	}
}
